﻿using System;
using System.Drawing;
using System.Windows.Forms;
using VoiceMessager.Stream;
using VoiceMessager.Stream.Cmd;

namespace VoiceMessager
{
	public partial class VoiceFrameForm : Form
	{
		private CmdStream _cmdStream;
		private SendStream _sendStream;
		private VerifyMethod _verifyMethod;
		private Udp _udp;

		public VoiceFrameForm(VerifyMethod verifyMethod,Udp udp)
		{
			InitializeComponent();
			_verifyMethod = verifyMethod;
			_udp = udp;
			comboBoxEncoding.DataSource = typeof(CmdCommandParam).GetEnumNames();
			comboBoxCommand.DataSource = typeof(CmdCommand).GetEnumNames();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			var location = new Point(buttonPreset.Left + this.Left, buttonPreset.Bottom + this.Top - contextMenuStrip1.Height);
			contextMenuStrip1.Show(location);
		}

		private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
		{
			this.textBoxInput.Text = e.ClickedItem.Text;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			this.textBoxInput.Text = string.Empty;
		}

		private void RefreshBytes()
		{
			if (string.IsNullOrEmpty(comboBoxCommand.Text)) return;
			if (string.IsNullOrEmpty(comboBoxEncoding.Text)) return;
			if (comboBoxCommand.Text == CmdCommand.PlayVoice.ToString())
			{
				buttonPreset.Enabled = true;
				textBoxInput.Enabled = true;
				if (string.IsNullOrEmpty(textBoxInput.Text))
				{
					buttonSend.Enabled = false;
					textBoxBytes.Clear();
				}
				else
				{
					textBoxInput.Enabled = true;
					comboBoxEncoding.Enabled = true;
					buttonSend.Enabled = true;
					var encoding = (CmdCommandParam) Enum.Parse(typeof(CmdCommandParam), comboBoxEncoding.Text);
					_cmdStream = CmdFactory.CreateStream(
						(CmdCommand) Enum.Parse(typeof(CmdCommand), comboBoxCommand.Text),
						encoding, Statics.GetEncodingBytes(encoding, textBoxInput.Text));
					_sendStream = SendFactory.Create(0, 1, _verifyMethod, _cmdStream);
					textBoxBytes.Text = Statics.ByteArrayToString(_sendStream.GetBytes());
				}
			}
			else
			{
				textBoxInput.Enabled = false;
				comboBoxEncoding.Enabled = false;
				buttonPreset.Enabled = false;
				_cmdStream = CmdFactory.CreateStream((CmdCommand) Enum.Parse(typeof(CmdCommand), comboBoxCommand.Text));
				_sendStream = SendFactory.Create(0, 1, _verifyMethod, _cmdStream);
				textBoxBytes.Text = Statics.ByteArrayToString(_sendStream.GetBytes());
			}
		}

		private void comboBoxCommand_SelectedIndexChanged(object sender, EventArgs e)
		{
			RefreshBytes();
		}

		private void comboBoxEncoding_SelectedValueChanged(object sender, EventArgs e)
		{
			RefreshBytes();
		}

		private void textBoxInput_TextChanged(object sender, EventArgs e)
		{
			RefreshBytes();
			if (_cmdStream.SendData.Length <= 240) return;
			textBoxInput.Text = textBoxInput.Text.Substring(0, textBoxInput.TextLength - 1);
			textBoxInput.SelectionStart = textBoxInput.TextLength;
		}

		private void buttonSend_Click(object sender, EventArgs e)
		{
			_udp.Send(_sendStream);
		}
	}
}
